package com.wt.admin.service.ai.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.code.BasicCode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.config.prop.IndexProp;
import com.wt.admin.domain.dto.ai.ModelConfigDTO;
import com.wt.admin.domain.entity.ai.ModelConfigEntity;
import com.wt.admin.domain.vo.ai.ModelConfigVO;
import com.wt.admin.mapper.ai.ModelConfigMapper;
import com.wt.admin.service.ai.ModelConfigService;
import com.wt.admin.util.AssertUtil;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;


@Service
public class ModelConfigServiceImpl extends ServiceImpl<ModelConfigMapper, ModelConfigEntity> implements ModelConfigService {

    @Resource
    private ModelConfigMapper modelConfigMapper;
    @Resource
    private IndexProp indexProp;

    @Override
    public PageUtil.PageVO<ModelConfigVO> configList(PageUtil.PageDTO<ModelConfigDTO> data) {
        return PageUtil.findPage(data, page -> modelConfigMapper.configList(page, data.getData()));
    }

    @Override
    public ModelConfigVO configEdit(ModelConfigDTO data) {
        AssertUtil.Boolean.isTrue(StrUtil.isBlank(data.getModel()), BasicCode.BASIC_CODE_99999);
        if(data.getTag().equals("EasyAI")){
            File qa = new File(String.format(indexProp.getModelPath().getQaModel(),data.getModel()));
            AssertUtil.Boolean.isTrue(!qa.exists(),BasicCode.BASIC_CODE_99999);
        }
        saveOrUpdate(data);
        return BeanUtil.copyProperties(data,ModelConfigVO.class);
    }

    @Override
    public ModelConfigVO configDel(ModelConfigDTO data) {
        removeById(data.getId());
        return null;
    }

    @Override
    public List<ModelConfigVO> configAll() {
        return modelConfigMapper.configAll();
    }

    @Override
    public ModelConfigEntity findById(Integer chatModel) {
        return modelConfigMapper.findById(chatModel);
    }

}
